﻿using ComputationalCore.Helper;

namespace BehaviorModel.Block.RandomGenerator
{
    /// <summary>
    /// Random generator with Gaussian strategy.
    /// </summary>
    public class GaussianRandomGenerator : IRandomGenerator
    {
        public double M { get; set; }
        public double Sigma { get; set; }

        public GaussianRandomGenerator()
        {
            M = 0;
            Sigma = 1;
        }

        public GaussianRandomGenerator(double m, double sigma)
        {
            M = m;
            Sigma = sigma;
        }

        #region Get Next

        public double GetNext(double prev)
        {
            return GetNext() + prev;
        }

        public double GetNext()
        {
            double randomSum = 0;

            do
            {
                randomSum = 0;
                for (int j = 0; j < 12; j++)
                {
                    randomSum += RandomHelper.NextDouble();
                }
                randomSum -= 6;
                randomSum = Sigma*randomSum + M;
            } while (randomSum < 0);

            return randomSum;
        }

        #endregion

    }
}
